<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械损益</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>

<body>
<style type="text/css">
	*{
		font-size:12px;
	}
	#QiXieProfitLossInfo{
		width:100%;
		overflow: hidden;
	}
	li{
		float: left;
		list-style: none;
	}
</style>
<script type="text/javascript">
$(function(){
	//根据仓库ID查询所有仓库详情表信息
	$('#qxsyckname').combogrid({
		onChange : function(newValue , oldValue){
			$('#qxsyckcpname').combogrid({
				url: 'selectHisInventoryById', 
				method: 'post', 
				queryParams: {
					ckNo: newValue
				}
			});
			$('#qxsyInfo').form('clear');
		}
	});
	
	//赋值
	$('#qxsyckcpname').combogrid({
		onChange : function(newValue , oldValue){
			var kcName=$('#qxsyckcpname').combogrid('getText');
			$.getJSON("selectHisInventoryByName", {"kcName": kcName}, function(data){
				/* $.getJSON("selectHisWarehouseByckNo", {"ckNo": data.ckNo}, function(data){
					$('#qxsyckname').textbox('setValue',data.ckName);
				}); */
				
				$('#qxsyjcsl').numberbox('setValue',data.kcNumber);
				var i=-parseInt(data.kcNumber);
				$('#qxsysl').numberspinner({max:data.kcNumber,min:i});
				$.getJSON("selectHisPurchaseByName", {"cpName": kcName}, function(data){
					$('#qxsycpph').textbox('setValue',data.cgBatchnumber);
				});
			});
			$('#qxsycpph').textbox('clear');
			$('#qxsyjcsl').numberbox('clear');
			$('#qxsysm').textbox('clear');
		}
	});
	
	//数字微调
	$('#qxsysl').numberspinner({
		onChange: function (newValue, oldValue) {
			var qxsyjcsl=$('#qxsyjcsl').numberbox('getValue');
			if(qxsyjcsl==''){
				$.messager.alert('温馨提示', '请先选择您需要报损益的产品名称!','info');
				$('#qxsysl').numberspinner('setValue',0);
				$('#qxsyjcsl').numberbox('setValue','');
			}else{
				var jcsl=$('#qxsyjcsl').numberbox('getValue');
				if(parseInt(newValue)==0 && parseInt(oldValue)==1){
					var o=parseInt(jcsl)+parseInt(oldValue);
					$('#qxsyjcsl').numberbox('setValue',o);
				}else if(parseInt(newValue)<=0){
					var o=parseInt(jcsl)-parseInt(oldValue)+parseInt(newValue);
					$('#qxsyjcsl').numberbox('setValue',o);
				}else{
					var o=parseInt(jcsl)+parseInt(oldValue)-parseInt(newValue);
					$('#qxsyjcsl').numberbox('setValue',o);
				}
			}
		}
	});
	
	//加入临时损益表
	$('#add-qxsyTable').linkbutton({
		onClick : function(){
			var qxsyckcpname=$('#qxsyckcpname').combogrid('getText');
			var qxsyckname=$('#qxsyckname').textbox('getText');
			var qxsycpph=$('#qxsycpph').textbox('getText');
			var qxsyjcsl=$('#qxsyjcsl').numberbox('getText');
			var qxsysl=$('#qxsysl').numberspinner('getValue');
			var qxsysm=$('#qxsysm').textbox('getText');
			var qxsyDate=$('#qxsyDate').datebox('getText');
			var qxsyAdmin=$('#qxsyAdmin').combogrid('getText');
			$.getJSON("selectHisPurchaseByName", {"cpName": qxsyckcpname}, function(data){
				var unit=data.cgUnit;
				var id=data.cgNo;
				var rows=$('#qxsytable').datagrid('getRows');
				var ii=0;
				for(var i=0;i<rows.length;i++){
					var name=rows[i]['hisProfitandlosses.cpName'];
					if(name==qxsyckcpname){
						ii++;
					}
				}
				if(qxsyDate=='' || qxsyAdmin=='' || qxsyckcpname=='' || parseInt(qxsysl)==0 ){
					$.messager.alert("温馨提示", "请把数据填充完整!",'info');
				}else if(ii>0){
					$.messager.alert("提示", "产品已存在临时损益中，请勿重复添加!");
					$('#qxsyInfo').form("clear");
				}else{
					$('#qxsytable').datagrid('appendRow', {
						'ckname' : qxsyckname,
						'cpNo' : id,
						'hisProfitandlosses.cpName' : qxsyckcpname,
						'hisProfitandlosses.ckNumber' : qxsyjcsl,
						'hisProfitandlosses.syNumber' : qxsysl,
						'unit' : unit,
						'hisProfitandlosses.cpPh' : qxsycpph,
						'hisProfitandlosses.sy_explain' : qxsysm
					});
					$('#qxsyInfo').form("clear");
				}
			});
		}
	});
	
	//损益
	$('#add-qxsyData').linkbutton({
		onClick : function(){
			//将datagrid中的数据行保存到隐式表单域中
			//1、获取新增的数据行
			var rows = $('#qxsytable').datagrid('getChanges');
			var today='${ProfitAndLossId}'*1;
			var qxsyDate=$('#qxsyDate').datebox('getText');
			var qxsyAdmin=$('#qxsyAdmin').combogrid('getValue');
			var ckName=$('#qxsyckname').textbox('getText');
			//alert(ckName);
			var ckID=undefined;
			$.getJSON("selectHisWarehouseByckName2", {"ckName": ckName}, function(data){
				ckID=data.ckNo;
				//alert(ckID);
				var ff_qixiesy = $("#ff_qixiesy");
				//为每一行生成对应的隐藏表单域
				for(var i = 0; i < rows.length; i ++){
					$("<input type='hidden' x='y'>").attr("name",
							"hisProfitandlosses[" + i + '].syNo').val(
									today+i).appendTo(ff_qixiesy);
					$("<input type='hidden' x='y'>").attr("name",
							"hisProfitandlosses[" + i + '].syDate').val(
									qxsyDate).appendTo(ff_qixiesy);
					$("<input type='hidden' x='y'>").attr("name",
							"hisProfitandlosses[" + i + '].ygNo').val(
									qxsyAdmin).appendTo(ff_qixiesy);
					$("<input type='hidden' x='y'>").attr("name",
							"hisProfitandlosses[" + i + '].ckNo').val(
									ckID).appendTo(ff_qixiesy);
					generateHidden(rows[i], "cpName", i);
					generateHidden(rows[i], "ckNumber", i);
					generateHidden(rows[i], "syNumber", i);
					generateHidden(rows[i], "cpPh", i);
					generateHidden(rows[i], "sy_explain", i);
				}
				//提交表单
				if(rows.length != 0){
					$('#ff_qixiesy').form('submit', {
						success: function(v){
							var json = eval("(" + v + ")");
							//alert(json);
							if(json=='操作成功'){
								$(":hidden[x='y']").remove();
								$('#qxsytable').datagrid('loadData', { total: 0, rows: [] });
								$.messager.alert('温馨提示', "损益"+json);
								$('#ff_qixiesy').form("clear");
								$("#win-QiXieProfitLoss").window("close");
							}else{
								$.messager.alert('温馨提示', json,'info');
							}
						}
					});
				}
			});
		}
	});
	
	function generateHidden(row, name, i) {
		var ff_qixiesy = $("#ff_qixiesy");
		$("<input type='hidden' x='y'>").attr("name",
				"hisProfitandlosses[" + i + '].'  + name).val(
				row['hisProfitandlosses.'+name]).appendTo(ff_qixiesy);
	}
});
</script>

	<!-- 器械损益 -->
	<!-- <div id="win-QiXieProfitLoss" class="easyui-window" title="器械损益" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-thpProfitLoss'"
		style="width: 650px; height: 400px;"> -->
		<form id="ff_qixiesy" action="addQiXieProfitAndLoss" method="post">
			<div id="QiXieProfitLossContent">
				<div id="QiXieProfitLossInfo" style="background-color: rgb(236,243,255)">
					<ul>
						<li style="margin:5px 0px 10px 0px;">损益日期&nbsp;<input id="qxsyDate" class="easyui-datebox" data-options="width: 100"></li>
						<li style="margin:5px 0px 10px 20px;">仓管员&nbsp;<select id="qxsyAdmin" class="easyui-combogrid" data-options="width: 100,
							url:'findAllEmployeeWarehouse',
							idField:'ygNo',
							panelHeight:'auto',
							panelWidth:225,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></select>
						</li>
						<li style="margin:5px 0px 0px 20px;">仓库&nbsp;<input id="qxsyckname" class="easyui-combogrid" data-options="width: 100,
							url:'selectAllHisWarehouse',
							idField:'ckNo',
							panelHeight:'auto',
							textField:'ckName',
							columns:[[
					            {field:'ckName',title:'仓库名称',width:'96px'}
							]]"></li>
						<li style="margin:5px 0px 10px 40px;"><a id="add-qxsyData" class="easyui-linkbutton" style="width:75px;">损益处理</a></li>
					</ul>
				</div>
				<div style="border-top:1px solid #95B8e7;width:100%;height:300px;overflow: hidden;">
					<div>
			        	<div id="qxsyInfo" style="overflow: hidden;">
			        		<ul>
			        			<li style="margin:10px 50px 0px 0px;">器械&nbsp;<select id="qxsyckcpname" class="easyui-combogrid" data-options="width: 188,
									idField:'kcNo',
									panelHeight:'auto',
									textField:'kcName',
									columns:[[{field:'kcName',title:'仓库器械名称',width:'180px'}
									]]"></select>
								</li>
								<li style="margin:10px 0px 0px 65px;">批号&nbsp;<input id="qxsycpph" class="easyui-textbox" data-options="width: 216" readonly="readonly"></li>
								<li style="margin:10px 32px 10px -24px;">结存数量&nbsp;<input id="qxsyjcsl" class="easyui-numberbox" data-options="width: 100" readonly="readonly"></li>
								<li style="margin:10px 30px 10px 0px;">损益数量&nbsp;<input id="qxsysl" title="-(负)报损,+(正)报益" value="0" class="easyui-numberspinner" data-options="width: 100" ></li>
								<li style="margin:5px 0px 10px 0px; color: red;">-(负)报损<br>+(正)报益</li>
								<li style="margin:0px 20px 10px -24px;">损益说明&nbsp;<input id="qxsysm" class="easyui-textbox" data-options="width: 450"></li>
								<li style="margin:0px 0px 10px 0px;"><a id="add-qxsyTable" class="easyui-linkbutton" style="width:75px;">加入</a></li>
							</ul>
			        	</div>
			        	<div style="border:0px red solid;height:170px;">
			        		<table id="qxsytable" class="easyui-datagrid"
								data-options="
									fitColumns:true,
									fit:true,
									singleSelect: true,
									rownumbers: true,
									toolbar: '#qxsytb',
									border:false
									">
								<thead>
									<tr>
										<th data-options="field:'ckname',width:'13%'">仓库名称</th>
										<th data-options="field:'cpNo',width:'13%'">器械编号</th>
										<th data-options="field:'hisProfitandlosses.cpName',width:'15%'">器械名称</th>
										<th data-options="field:'hisProfitandlosses.ckNumber',width:'10%'">结存数量</th>
										<th data-options="field:'hisProfitandlosses.syNumber',width:'10%'">损益数量</th>
										<th data-options="field:'unit',width:'8%'">单位</th>
										<th data-options="field:'hisProfitandlosses.cpPh',width:'12%'">批号</th>
										<th data-options="field:'hisProfitandlosses.sy_explain',width:'21%'">说明</th>
									</tr>
								</thead>
							</table>
							<div id="qxsytb" style="height: auto;padding-left: 30px;">
								<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-add',plain:true">添加一行</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-remove',plain:true"
									onclick="removeit()">移除一行</a> <a href="javascript:void(0)"
									class="easyui-linkbutton"
									data-options="iconCls:'icon-ok',plain:true">应用</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
							</div>
			        	</div>
			        </div>
			    </div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>